package org.lq.er.view.exam_manage;


import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.lq.er.entity.Exam_position;
import org.lq.er.service.Exam_positionService;
import org.lq.er.service.impl.Exam_positionServiceImpl;

import javax.swing.JPopupMenu;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class V_examPosition extends JFrame {

	private JPanel contentPane;
	private static JTable table;
	private Exam_positionService ps = new Exam_positionServiceImpl();
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_examPosition frame = new V_examPosition();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_examPosition() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 534, 413);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 528, 384);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JButton btnNewButton_1 = new JButton("导出");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				exportExcel();				
			}
		});
		btnNewButton_1.setBounds(13, 5, 65, 23);
		panel.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("刷新");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//----------------初始化表格，载入数据-------------------------------
				initTable();
			}
		});
		btnNewButton_2.setBounds(108, 6, 65, 23);
		panel.add(btnNewButton_2);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(0, 34, 528, 340);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("考场管理");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 16));
		lblNewLabel.setBounds(10, 10, 78, 25);
		panel_1.add(lblNewLabel);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 31, 496, 299);
		panel_1.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{"1", "10#2201", "30"},
			},
			new String[] {
				"\u5E8F\u53F7", "\u8003\u573A\u540D", "\u8003\u573A\u5927\u5C0F"
			}
		));
		scrollPane.setViewportView(table);
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("删除");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//-----------------------删除考场----------------------------------
				int rowIndex = table.getSelectedRow();
				Object idstr = table.getValueAt(rowIndex, 0);
				int id = Integer.parseInt(idstr.toString());
				if (ps.delete(id)) {
					
					JOptionPane.showMessageDialog(V_examPosition.this, "恭喜删除成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
					
					initTable();
				}else {
					
					JOptionPane.showMessageDialog(V_examPosition.this, "删除失败！", "错误", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		popupMenu.add(mntmNewMenuItem_1);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("修改");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			//--------------修改考场数据------------------
				// 点击修改事件
				Exam_position expo = new Exam_position();
				// 获取点击的行数
				int rowIndex = table.getSelectedRow();
				// 获取点击行的各列的值
				String expno = table.getValueAt(rowIndex, 0).toString();
				String exposition = table.getValueAt(rowIndex, 1).toString();
				String expnum = table.getValueAt(rowIndex, 2).toString();
				
				expo.setId(Integer.parseInt(expno));
				expo.setPosition(exposition);
				expo.setMax_count(Integer.parseInt(expnum));
				
				V_examPosition_update update = new V_examPosition_update(expo);
				update.setBounds(V_examPosition.this.getX()+100, V_examPosition.this.getY(), update.getWidth(), update.getHeight());
				update.setVisible(true);
				
				// 监听窗体是否被关闭
				update.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						//当添加窗体关闭的时候,调用表格初始化方法
						initTable();
					}
					@Override
					public void windowClosing(WindowEvent e) {
					}
				});
			}
		});
		popupMenu.add(mntmNewMenuItem);
		
		JMenuItem mntmNewMenuItem_2 = new JMenuItem("添加");
		mntmNewMenuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			//--------------添加一个考场数据------------------
				V_examPosition_add add = new V_examPosition_add();
				add.setBounds(V_examPosition.this.getX()+100, V_examPosition.this.getY(), add.getWidth(), add.getHeight());
				add.setVisible(true);
				
				// 监听窗体是否被关闭
				add.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						//当添加窗体关闭的时候,调用表格初始化方法
						initTable();
					}
					@Override
					public void windowClosing(WindowEvent e) {
					}
				});
			}
		});
		popupMenu.add(mntmNewMenuItem_2);
	}
	/**
	 * 	右键菜单操作
	 * @param component
	 * @param popup
	 */
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				int index = table.rowAtPoint(e.getPoint());
				popup.show(e.getComponent(), e.getX(), e.getY());
				table.setRowSelectionInterval(index, index);
			}
		});
	}
	/**
	 * 	初始化表格数据
	 * 
	 */
	public void initTable() {
		
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		//清楚数据
		int count = dtm.getRowCount();
		for (int i = 0; i < count; i++) {
			dtm.removeRow(0);
		}
		//在添加数据之前，先清空表中数据
		List<Exam_position> list = ps.findAll();
		for (Exam_position exp : list) {
			dtm.addRow(new Object [] {
					exp.getId(),
					exp.getPosition(),
					exp.getMax_count(),
			});
		}
		
	}
	/**
	 * 导出考场信息
	 */
	public void exportExcel() {
		// 创建工作簿对象 
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 创建工作表对象
		HSSFSheet sheet = workbook.createSheet();
		workbook.setSheetName(0, "考场信息");
		// 创建工作表的行
		HSSFRow row = sheet.createRow(0);// 设置第一行
		
		HSSFCell cell = row.createCell(0);//创建单元格
		cell.setCellValue("考场序号");//设置单元格内的内容
		
		row.createCell(1).setCellValue("考场位置");
		row.createCell(2).setCellValue("考场人数");
		List<Exam_position> list = ps.findAll();
		for (int i = 0; i < list.size(); i++) {
			Exam_position exp = list.get(i);
			HSSFRow row2 = sheet.createRow(i+1);
			row2.createCell(0).setCellValue(exp.getId());
			row2.createCell(1).setCellValue(exp.getPosition());
			row2.createCell(2).setCellValue(exp.getMax_count());
			
		}		
		String fileName = "考场数据信息"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".xls";
		// 创建JFileChooser
		JFileChooser jf = new JFileChooser();
		jf.setSelectedFile(new File(fileName));
		int result = jf.showSaveDialog(null);
		File path = jf.getSelectedFile();
		try {
			if (result == 0) {
				workbook.write(new File(path+fileName));
			} else {
				workbook.write(new File("C:\\Users\\Administrator\\Desktop\\"+fileName));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
